package com.kingdee.eas.hr.emp.app;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.person.IPerson;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.base.SpecialityInfo;
import com.kingdee.eas.hr.emp.PersonDegreeInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.util.StringUtils;

public class PersonDegreeControllerBeanEx extends PersonDegreeControllerBean {

	protected IObjectPK _addnew(Context ctx, IObjectValue model)
			throws BOSException, EASBizException {
		PersonDegreeInfo personDegreeInfo = (PersonDegreeInfo) model;

		if (personDegreeInfo.isChkIsJoinDegree()) {
			DbUtil.execute(ctx,
					"UPDATE T_HR_PersonDegree SET CFChkIsJoinDegree = 0 WHERE FPersonId='"
							+ personDegreeInfo.getPerson().getId()
							+ "' AND FID<>'" + personDegreeInfo.getId() + "'");

			IPerson iP = PersonFactory.getLocalInstance(ctx);
			PersonInfo pInfo = iP.getPersonInfo(new ObjectUuidPK(
					personDegreeInfo.getPerson().getId()));
			pInfo.setJoinDiploma(personDegreeInfo.getDiploma());
			pInfo.setGraduateDate(personDegreeInfo.getGraduateDate());
			pInfo.setGraduateSchool(personDegreeInfo.getGraduateSchool());
			pInfo.setSpeciality(personDegreeInfo.getSpecialty());
			iP.updatePartial(pInfo, this.getSelectorItemCollection());
		}
		return super._addnew(ctx, model);
	}

	protected void _delete(Context ctx, IObjectPK pk) throws BOSException,
			EASBizException {
		PersonDegreeInfo personDegreeInfo = getPersonDegreeInfo(ctx, pk);
		if (personDegreeInfo.isChkIsJoinDegree()) {
			IPerson iP = PersonFactory.getLocalInstance(ctx);
			PersonInfo pInfo = iP.getPersonInfo(new ObjectUuidPK(
					personDegreeInfo.getPerson().getId()));
			pInfo.setJoinDiploma(null);
			pInfo.setGrade(null);
			iP.updatePartial(pInfo, getSelectorItemCollection());
		}
		super._delete(ctx, pk);
	}

	protected void _update(Context ctx, IObjectPK pk, IObjectValue model)
			throws BOSException, EASBizException {
		PersonDegreeInfo personDegreeInfo = (PersonDegreeInfo) model;
		if (personDegreeInfo.isChkIsJoinDegree()) {
			DbUtil.execute(ctx,
					"UPDATE T_HR_PersonDegree SET CFChkIsJoinDegree = 0 WHERE FPersonId='"
							+ personDegreeInfo.getPerson().getId() + "'");

			IPerson iP = PersonFactory.getLocalInstance(ctx);
			PersonInfo pInfo = iP.getPersonInfo(new ObjectUuidPK(
					personDegreeInfo.getPerson().getId()));
			pInfo.setJoinDiploma(personDegreeInfo.getDiploma());
			pInfo.setGraduateDate(personDegreeInfo.getGraduateDate());
			pInfo.setGraduateSchool(personDegreeInfo.getGraduateSchool());
			pInfo.setSpeciality(personDegreeInfo.getSpecialty());
			iP.updatePartial(pInfo, getSelectorItemCollection());
		}
		super._update(ctx, pk, model);
	}

	private SelectorItemCollection getSelectorItemCollection() {
		SelectorItemCollection selectors = new SelectorItemCollection();
		selectors.add("joinDiploma");
		selectors.add("graduateSchool");
		selectors.add("graduateDate");
		selectors.add("specialty");
		return selectors;
	}

	protected boolean _isDegreeRepeat(Context ctx, IObjectValue personDegreeInfo)
			throws BOSException, EASBizException {
		return isDegreeDup(ctx, (PersonDegreeInfo) personDegreeInfo);
	}

	private boolean isDegreeDup(Context ctx, PersonDegreeInfo info)
			throws EASBizException, BOSException {
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("degree.id", info.getDegree() == null ? null
						: ((Object) (info.getDegree().getId())),
						CompareType.EQUALS));
		filter.getFilterItems().add(
				new FilterItemInfo("diploma.id",
						info.getDiploma() == null ? null : ((Object) (info
								.getDiploma().getId())), CompareType.EQUALS));
		filter.getFilterItems().add(
				new FilterItemInfo("specialtyType.id",
						info.getSpecialtyType() == null ? null
								: ((Object) (info.getSpecialtyType().getId())),
						CompareType.EQUALS));
		filter.getFilterItems().add(
				new FilterItemInfo("degreeCountry.id",
						info.getDegreeCountry() == null ? null
								: ((Object) (info.getDegreeCountry().getId())),
						CompareType.EQUALS));
		filter.getFilterItems().add(
				new FilterItemInfo("person.id", info.getPerson().getId(),
						CompareType.EQUALS));
		int maskNum = 5;
		StringBuffer maskStr = new StringBuffer();
		String degreeUnit = info.getDegreeUnit();
		SpecialityInfo specialty = info.getSpecialty();
		if (StringUtils.isEmpty(degreeUnit)) {
			filter.getFilterItems().add(
					new FilterItemInfo("degreeUnit", null, CompareType.EQUALS));
			filter.getFilterItems().add(
					new FilterItemInfo("degreeUnit", "", CompareType.EQUALS));
			maskStr.append(" and (#" + maskNum + " or #" + (maskNum + 1) + ")");
			maskNum += 2;
		} else {
			filter.getFilterItems().add(
					new FilterItemInfo("degreeUnit", degreeUnit,
							CompareType.EQUALS));
			maskStr.append(" and #" + maskNum);
			maskNum++;
		}
		if (specialty == null) {
			filter.getFilterItems().add(
					new FilterItemInfo("specialty.id", null,
							CompareType.EQUALS));
			filter.getFilterItems().add(
					new FilterItemInfo("specialty.id", null,
							CompareType.EQUALS));
			maskStr.append(" and (#" + maskNum + " or #" + (maskNum + 1) + ")");
			maskNum += 2;
		} else {
			filter.getFilterItems().add(
					new FilterItemInfo("specialty.id", specialty.getId()
							.toString(), CompareType.EQUALS));
			maskStr.append(" and #" + maskNum);
			maskNum++;
		}
		if (info.getId() != null) {
			filter.getFilterItems().add(
					new FilterItemInfo("id", info.getId(),
							CompareType.NOTEQUALS));
			maskStr.append(" and #" + maskNum);
			maskNum++;
		}
		filter.setMaskString("#0 and #1 and #2 and #3 and #4 "
				+ maskStr.toString());
		return !_exists(ctx, filter);
	}

}
